
package com.xdf.wxbebarrel.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import com.xdf.wxbebarrel.dao.flux.UserPermissionMapper;
import com.xdf.wxbebarrel.model.UserPermission;
import com.xdf.wxbebarrel.utils.BusinessException;

/**
 * @Description : 服务层实现类，由代码生成器自动生成
 * @Author : chanchaw
 * @Date : 2022-10-22 13:59:39
 */
@Service
public class UserPermissionServiceImpl implements UserPermissionService {
    @Autowired
    private UserPermissionMapper dao;

    @Override
    public int insert(UserPermission record) {
        check4Insert(record);
        return dao.insert(record);
    }

    @Override
    public int insertSelective(UserPermission record) {
        check4Insert(record);
        return dao.insertSelective(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<UserPermission> list) {
        return dao.multiInsert(list);
    }

    @Override
    public int updateByPrimaryKey(UserPermission record) {
        return dao.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(UserPermission record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dao.deleteByPrimaryKey(id);
    }

    @Override
    public UserPermission selectByPrimaryKey(Integer id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public List<UserPermission> selectAll(){
        return dao.selectAll();
    }

    @Override
    public int save(UserPermission record){
        check4Insert(record);
        return dao.save(record);
    }

    // 没有使用 xml 中的 multiSave 方法是因为无法批量回写自增主键
    @Transactional
    @Override
    public int multiSave(List<UserPermission> list){
        if(list == null || list.size()==0) return 0;
        list.forEach(item -> dao.save(item));
        return list.size();
    }

    /**=========================下面开始是固定模式代码=======================================*/
    public UserPermission selectBySid(String sid){
        return dao.selectBySid(sid);
    }

    // 返回true表示存在，false表示不存在
    public Boolean exists(String sid){
        UserPermission record = selectBySid(sid);
        return record == null ? false : true;
    }

    // 新增前检测，不可新增则抛出异常
    private void check4Insert(UserPermission record){
        if(record == null) throw new BusinessException("新增的对象不可为空！");
//        String sid = record.getSid();
//        if(sid == null) throw new BusinessException("编号不可为空！");
//        if(exists(sid)) throw new BusinessException("编号" + sid + "已经存在，请更换后再次保存！");
    }
    //=================以上是自动生成的代码，自定义代码请写在下面==================
}